<template>
  <basic-container>
    <avue-crud ref="crud" v-model="form" :before-open="beforeOpen" :data="data" :option="option" :page.sync="page"
      @row-click="rowClick" :permission="permissionList" :table-loading="loading" @row-update="rowUpdate"
      @row-save="rowSave" @row-del="rowDel" @search-change="searchChange" @search-reset="searchReset"
      @selection-change="selectionChange" @current-change="currentChange" @size-change="sizeChange" @on-load="onLoad">
      <template slot="menu" slot-scope="{row}">
        <el-button @click="view(row)" icon="el-icon-view" type="text">查看</el-button>
      </template>
      <template slot="text2Form">
        <avue-crud ref="crud" v-model="form2" :before-open="beforeOpen" :data="data2" :option="option2" :page.sync="page"
          :permission="permissionList" :table-loading="loading" @on-load="onLoad2" @tree-load="treeLoad">
          <template slot-scope="{type,size,row,index}" slot="menu">
            <el-button @click="viewHandler(row)" icon="el-icon-view" type="text">查看</el-button>
            <el-button @click="download(row, index)" icon="el-icon-download" type="text"
              v-if="!row.filesList">下载</el-button>
          </template>
        </avue-crud>
      </template>
    </avue-crud>
    <el-dialog title="查看" append-to-body :visible.sync="box" width="40%">
      <avue-form ref="form" :option="viewOption" v-model="viewForm" />
    </el-dialog>
  </basic-container>
</template>

<script>
import {
  getList, getList2, getDetail, add, update, remove, exportBlob
  , downloadXls
} from "@/api/business_process/caseInfo";
import { mapGetters } from "vuex";
import website from '@/config/website';
import { getToken } from '@/util/auth.js';
export default {
  data() {
    return {
      box: false,
      viewForm: {},
      data2: [],
      form: {},
      query: {},
      loading: true,
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0
      },
      selectionList: [],
      option: {

        height: 'auto',
        calcHeight: 210,
        searchShow: true,
        searchMenuSpan: 6,
        tip: false,
        border: true,
        index: true,
        tabs: true,
        tabsActive: 2,
        viewBtn: true,
        // selection: true,
        labelWidth: 180,
        indexLabel: '序号',
        addBtn: false,
        indexWidth: 80,
        refreshBtn: false,
        selection: false,  //多选框
        columnBtn: false,
        delBtn: false,
        viewBtn: false,
        editBtn: false,
        searchShowBtn: false,
        column: [

          {
            label: "申请事项名称",
            prop: "deptQlName",
            viewDisplay: false,
            search: true
          },
          {
            label: "部门名称",
            prop: "orgName",
            viewDisplay: false,
            search: true
          },
          {
            label: "全省唯一办件编号",
            prop: "internalNo",
            viewDisplay: false,
            search: true
          },

          {
            label: "申请人名称",
            prop: "applicantName",
            viewDisplay: false,
            search: true
          },
          {
            label: "网上申请/收件时间",
            prop: "wapplyDate",
            viewDisplay: false,
          },
          {
            label: "证件号码",
            prop: "applicantPaperCode",
            viewDisplay: false,
            search: true
          },
          {
            label: "承诺办结时间",
            prop: "promiseDate",
            viewDisplay: false,
          },
          {
            label: "数据来源",
            prop: "dataSources",
            hide: true,
            viewDisplay: false,
            search: true,
            type: "select",
            dicUrl: '/api/blade-system/dict-biz/dictionary?code=data_sources',
            props: {
              label: "dictValue",
              value: "dictKey"
            },
          },
          {
            label: "办件状态",
            prop: "bjStatus",
            hide: true,
            viewDisplay: false,
            search: true,
            type: "select",
            dicUrl: '/api/blade-system/dict-biz/dictionary?code=case_status',
            props: {
              label: "dictValue",
              value: "dictKey"
            },
          },
          {
            label: "办结时间",
            prop: "bjDate",
            viewDisplay: false
          },
        ],
        group: [
          {
            icon: 'el-icon-info',
            label: '申请信息',
            prop: 'group1',
            column: [
              {
                label: "行政区划编码",
                prop: "areaNo",
                hide: true,
              },
              {
                label: "行政区划名称",
                prop: "areaName",
                hide: true,
              },
              {
                label: "部门编码（部门的统一社会信用代码）",
                prop: "orgId",
                hide: true,
              },
              {
                label: "部门名称",
                prop: "orgName",
                hide: true,
              },

              {
                label: "全省唯一办件编号",
                prop: "internalNo",
              },
              {
                label: "事项编码",
                prop: "deptQlRegNo",
                hide: true,
              },
              {
                label: "申请事项名称",
                prop: "deptQlName",
              },
              {
                label: "业务办理项编码",
                prop: "deptYwRegNo",
                hide: true,
              },
              {
                label: "申请业务名称",
                prop: "deptYwName",
              },
              {
                label: "经办处室",
                hide: true,
                prop: "department",
              },
              {
                label: "办件名称",
                prop: "transactAffairName",
                hide: true,
              },
              {
                label: "办件摘要",
                prop: "content",
                hide: true,
              },
              {
                label: "是否加急",
                prop: "ifUrgent",
                hide: true,
                type: "select",
                dicUrl: '/api/blade-system/dict-biz/dictionary?code=ifUrgent',
                props: {
                  label: "dictValue",
                  value: "dictKey"
                },
              },
              {
                label: "申请人类型",
                prop: "applicantType",
                hide: true,
                type: "select",
                dicUrl: '/api/blade-system/dict-biz/dictionary?code=applicant_type',
                props: {
                  label: "dictValue",
                  value: "dictKey"
                },
              },
              {
                label: "申请人名称",
                prop: "applicantName",
              },
              {
                label: "证件类型",
                prop: "applicantPaperType",
                hide: true,
                type: "select",
                dicUrl: '/api/blade-system/dict-biz/dictionary?code=document_type',
                props: {
                  label: "dictValue",
                  value: "dictKey"
                },
              },
              {
                label: "证件号码",
                prop: "applicantPaperCode",
                hide: true,
              },
              {
                label: "手机",
                prop: "applicantMobile",
                hide: true,
              },
              {
                label: "联系电话",
                prop: "applicantPhone",
                hide: true,
              },
              {
                label: "联系地址",
                prop: "appliantAddress",
                hide: true,
              },
              {
                label: "邮政编码",
                prop: "applicantZipcode",
                hide: true,
              },
              {
                label: "电子邮箱",
                prop: "applicantEmail",
                hide: true,
              },
              {
                label: "组织机构代码",
                prop: "applicantCode",
                hide: true,
              },
              {
                label: "法定代表人姓名",
                prop: "operManName",
                hide: true,
              },
              {
                label: "申请方经办人姓名",
                prop: "linkmanName",
                hide: true,
              },
              {
                label: "申请方经办人证件类型",
                prop: "linkmanPaperType",
                hide: true,
                type: "select",
                dicUrl: '/api/blade-system/dict-biz/dictionary?code=document_type',
                props: {
                  label: "dictValue",
                  value: "dictKey"
                },
              },
              {
                label: "申请方经办人证件号码",
                prop: "linkmanPaperCode",
                hide: true,
              },
              {
                label: "申请方经办人手机",
                prop: "linkmanMobile",
                hide: true,
              },
              {
                label: "申请方经办人联系电话",
                prop: "linkmanPhone",
                hide: true,
              },
              {
                label: "申请方经办人联系地址",
                prop: "linkmanAddress",
                hide: true,
              },
              {
                label: "申请方经办人邮政编码",
                prop: "linkmanZipcode",
                hide: true,
              },
              {
                label: "申请方经办人邮箱地址",
                prop: "linkmanEmail",
                hide: true,
              },
              {
                label: "办件业务说明",
                prop: "yeMs",
                hide: true,
              },
              {
                label: "办件附件说明",
                prop: "sjFileRemark",
                hide: true,
              },
              {
                label: "法定办结时限",
                prop: "anticipate",
                hide: true,
              },
              {
                label: "法定办结时限计量单位",
                prop: "anticipateDayType",
                hide: true,
                type: "select",
                dicUrl: '/api/blade-system/dict-biz/dictionary?code=anticipate_type',
                props: {
                  label: "dictValue",
                  value: "dictKey"
                },
              },
              {
                label: "承诺办结时限",
                prop: "promise",
                hide: true,
              },
              {
                label: "承诺办结时限计量单位",
                prop: "promiseType",
                hide: true,
                type: "select",
                dicUrl: '/api/blade-system/dict-biz/dictionary?code=anticipate_type',
                props: {
                  label: "dictValue",
                  value: "dictKey"
                },
              },
              {
                label: "承诺办结时间",
                prop: "promiseDate",
                hide: true,
              },
              {
                label: "法定办结时间",
                prop: "anticipateDate",
                hide: true,
              },
              {
                label: "网上申请/收件时间",
                prop: "wapplyDate",
                hide: true,
              },
              {
                label: "受理时间",
                prop: "applyDate",
                hide: true,
              },
              {
                label: "办件状态",
                prop: "bjStatus",
                hide: true,
                type: "select",
                dicUrl: '/api/blade-system/dict-biz/dictionary?code=case_status',
                props: {
                  label: "dictValue",
                  value: "dictKey"
                },
              },
              {
                label: "数据来源",
                prop: "dataSources",
                hide: true,
                type: "select",
                dicUrl: '/api/blade-system/dict-biz/dictionary?code=data_sources',
                props: {
                  label: "dictValue",
                  value: "dictKey"
                },
              },
              {
                label: "办件类型",
                prop: "applyType",
                hide: true,
                type: "select",
                dicUrl: '/api/blade-system/dict-biz/dictionary?code=apply_type',
                props: {
                  label: "dictValue",
                  value: "dictKey"
                },
              },
              {
                label: "受理文书编号",
                prop: "applyDocno",
                hide: true,
              },
              {
                label: "事项类型",
                prop: "qlKind",
                hide: true,
                type: "select",
                dicUrl: '/api/blade-system/dict-biz/dictionary?code=task_type',
                props: {
                  label: "dictValue",
                  value: "dictKey"
                },
              },
              {
                label: "申请方式",
                prop: "sqWay",
                hide: true,
                type: "select",
                dicData: [
                  { label: "PC 端外网申报", value: "1" },
                  { label: "APP申报", value: "2" },
                  { label: "综合窗口线下收件", value: "3" },
                ],
              },
              {
                label: "投资项目编号",
                prop: "rojectExamNo",
                hide: true,
              },
              {
                label: "办结时间",
                prop: "bjDate",
                hide: true,
              },
              {
                label: "受理人",
                prop: "casereceiveer",
                hide: true,
              },
              {
                label: "是否发证",
                prop: "isCert",
                hide: true,
                type: "select",
                dicUrl: '/api/blade-system/dict-biz/dictionary?code=yes_no',
                props: {
                  label: "dictValue",
                  value: "dictKey"
                },
              },
              {
                label: "是否收费",
                prop: "isCharge",
                hide: true,
                type: "select",
                dicUrl: '/api/blade-system/dict-biz/dictionary?code=yes_no',
                props: {
                  label: "dictValue",
                  value: "dictKey"
                },
              },
              {
                label: "收费状态",
                prop: "caseFeeStatus",
                hide: true,
                type: "select",
                dicData: [
                  { label: "已收费", value: "1" },
                  { label: "未收费", value: "2" }
                ],
              },
              {
                label: "容缺受理承诺日期",
                prop: "lackPromiseDate",
                hide: true,
              },
              {
                label: "是否容缺受理",
                prop: "whetherLackAcceptance",
                hide: true,
                type: "select",
                dicUrl: '/api/blade-system/dict-biz/dictionary?code=yes_no',
                props: {
                  label: "dictValue",
                  value: "dictKey"
                },
              },
              {
                label: "容缺受理天数",
                prop: "lackAcceptanceDay",
                hide: true,
              },
              {
                label: "是否结果快递",
                prop: "caseExpress",
                hide: true,
                type: "select",
                dicUrl: '/api/blade-system/dict-biz/dictionary?code=yes_no',
                props: {
                  label: "dictValue",
                  value: "dictKey"
                },
              },
              {
                label: "寄件内容",
                prop: "expressType",
                hide: true,
                type: "select",
                dicData: [
                  { label: "申请材料寄送", value: "1" },
                  { label: "审批结果寄送", value: "2" }
                ],
              },
              {
                label: "事项版本唯一标识",
                prop: "taskItemId",
                hide: true,
              },
              {
                label: "法定代表人证件类型",
                prop: "operManType",
                hide: true,
                type: "select",
                dicUrl: '/api/blade-system/dict-biz/dictionary?code=document_type',
                props: {
                  label: "dictValue",
                  value: "dictKey"
                },
              },
              {
                label: "法定代表人证件号码",
                prop: "operManCode",
                hide: true,
              },
              {
                label: "法定代表人电话号码",
                prop: "operManPhone",
                hide: true,
              },
            ]
          }, {
            icon: 'el-icon-info',
            label: '材料信息',
            prop: 'group2',
            column: [
              {
                label: '',
                labelWidth: 0,
                prop: 'text2',
                slot: true,
                span: 24,
              }
            ]
          }]
      },
      option2: {
        rowKey: 'id',
        lazy: true,
        tree: true,
        height: 'auto',
        calcHeight: 210,
        searchShow: true,
        searchMenuSpan: 6,
        tip: false,
        border: true,
        index: true,
        tabs: true,
        tabsActive: 2,
        menu: true,
        viewBtn: false,
        // selection: true,
        labelWidth: 180,
        indexLabel: '序号',
        addBtn: false,
        indexWidth: 80,
        refreshBtn: false,
        selection: false,  //多选框
        columnBtn: false,
        delBtn: false,
        editBtn: false,
        searchShowBtn: false,
        column: [
          {
            label: "材料名称",
            prop: "materialName",
          },
        ],
      },
      data: [],
      rowData: {},
      viewOption: {
        detail: true,
        submitBtn: false,
        emptyBtn: false,
        column: [],
      },
      column1: [
        {
          label: '材料名称',
          prop: 'materialName',
          span:12,
        },
        {
          label: '全省唯一办件编号',
          prop: 'internalNo',
          span:12,
        },
        {
          label: '材料必要性',
          prop: 'isNeed',
          span:12,
          type: "select",
          dicUrl: '/api/blade-system/dict-biz/dictionary?code=mate_necessary',
          props: {
            label: "dictValue",
            value: "dictKey"
          },
        },
        {
          label: '材料类型',
          prop: 'materialType',
          span:12,
          type: "select",
          dicUrl: '/api/blade-system/dict-biz/dictionary?code=mate_type',
          props: {
            label: "dictValue",
            value: "dictKey"
          },
        },
        {
          label: '排序号',
          prop: 'orderNum',
          span:12,
        },
        {
          label: '材料版本标识',
          prop: 'materialId',
          span:12,
        },
        {
          label: '材料唯一标识',
          prop: 'materialGuid',
          span:12,
        },
        {
          label: '来源渠道',
          prop: 'sourceType',
          span:12,
          type: "select",
          dicUrl: '/api/blade-system/dict-biz/dictionary?code=source_type',
          props: {
            label: "dictValue",
            value: "dictKey"
          },
        },
        {
          label: '材料形式',
          prop: 'materialForm',
          span:12,
          type: "select",
          dicUrl: '/api/blade-system/dict-biz/dictionary?code=mate_form',
          props: {
            label: "dictValue",
            value: "dictKey"
          },
        },
        {
          label: '事项版本标识',
          prop: 'taskItemId',
          span:12,
        },
        {
          label: '材料收取形式',
          prop: 'collectionForm',
          span:12,
          type: "select",
          dicUrl: '/api/blade-system/dict-biz/dictionary?code=mate_sqxs',
          props: {
            label: "dictValue",
            value: "dictKey"
          },
        },
      ],
      column2: [
        {
          label: '附件名称',
          prop: 'fileName',
          span:12,
        },
        {
          label: '全省唯一办件编号',
          prop: 'internalNo',
          span:12,
        },
        {
          label: '附件唯一编码',
          prop: 'fileUniqueCode',
          span:12,
        },
        {
          label: '附件下载地址',
          prop: 'fileUrl',
          span:12,
        },
        {
          label: '附件文件类型',
          prop: 'fileType',
          span:12,
        },
        {
          label: '材料版本标识',
          prop: 'materialId',
          span:12,
        },
        {
          label: '材料名称',
          prop: 'materialName',
          span:12,
        },
        {
          label: '上传人姓名',
          prop: 'uploadUser',
          span:12,
        },
        {
          label: '上传日期',
          prop: 'uploadDate',
          span:12,
        },
        {
          label: '是否是电子证照',
          prop: 'isCert',
          span:12,
          type: "select",
          dicUrl: '/api/blade-system/dict-biz/dictionary?code=ifUrgent',
          props: {
            label: "dictValue",
            value: "dictKey"
          },
        },
        {
          label: '颁证单位统一社会信用代码',
          prop: 'issueDept',
          span:12,
        },
        {
          label: '证照来源',
          prop: 'certSource',
          span:12,
          type: "select",
          dicUrl: '/api/blade-system/dict-biz/dictionary?code=source_type',
          props: {
            label: "dictValue",
            value: "dictKey"
          },
        },
        {
          label: '证照编码',
          prop: 'certCode',
          span:12,
        },
      ],
    };
  },
  computed: {
    ...mapGetters(["permission"]),
    ids() {
      let ids = [];
      this.selectionList.forEach(ele => {
        ids.push(ele.id);
      });
      return ids.join(",");
    }
  },
  methods: {
    viewHandler(row) {
      console.log('22222222222', row);
      this.viewForm = row
      if (row.filesList) {
        // 父极
        this.viewOption.column = this.column1
      } else {
        this.viewOption.column = this.column2
      }
      this.box = true
    },
    download(row) {
      console.log('row=====', row);
      window.open(row.fileUrl)
      // exportBlob(
      //     `${row.fileUrl}?${this.website.tokenHeader}=${getToken()}`
      //   ).then(res => {
      //     downloadXls(res.data, `${row.fileName}`);
      //   });
    },
    treeLoad(tree, treeNode, resolve) {
      let res = this.data2.find(item => item.id === tree.id)
      console.log('res=====', res);
      if (res) {
        // let rename = res.filesList.map(item => ({ ...item, materialName: item.fileName }))
        let rename = res.filesList
        resolve(rename);
      }

    },
    view(row) {
      this.rowData = row
      this.$refs.crud.rowView(row)
      console.log('row0000', row);
    },
    rowSave(row, done, loading) {
      add(row).then(() => {
        done();
        this.onLoad(this.page);
        this.$message({
          type: "success",
          message: "操作成功!"
        });
      }, error => {
        window.console.log(error);
        loading();
      });
    },
    rowUpdate(row, index, done, loading) {
      update(row).then(() => {
        done();
        this.onLoad(this.page);
        this.$message({
          type: "success",
          message: "操作成功!"
        });
      }, error => {
        window.console.log(error);
        loading();
      });
    },
    rowDel(row) {
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          return remove(row.id);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        });
    },
    handleDelete() {
      if (this.selectionList.length === 0) {
        this.$message.warning("请选择至少一条数据");
        return;
      }
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          return remove(this.ids);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          this.$refs.crud.toggleSelection();
        });
    },
    beforeOpen(done, type) {
      if (["edit", "view"].includes(type)) {
        getDetail(this.form.id).then(res => {
          this.form = res.data.data;
        });
      }
      done();
    },
    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },
    searchChange(params, done) {
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    selectionChange(list) {
      this.selectionList = list;
    },
    selectionClear() {
      this.selectionList = [];
      this.$refs.crud.toggleSelection();
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    onLoad(page, params = {}) {
      this.loading = true;
      getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
        const data = res.data.data;
        this.page.total = data.total;
        this.data = data.records;
        this.loading = false;
        this.selectionClear();
      });
    },
    onLoad2(page, params = {}) {
      this.loading = true;
      getList2(page.currentPage, page.pageSize, Object.assign(params, this.query, { internalNo: this.rowData.internalNo })).then(res => {
        console.log('2222', res);
        const data = res.data.data;
        this.page.total = data.total;
        this.data2 = data.records.map(item => ({ ...item, hasChildren: item.hasChildren === 'false' ? false : true }));
        console.log('data2', this.data2);
        this.loading = false;
        this.selectionClear();
      });
    }
  }
};
</script>

<style scoped></style>

